package com.LittleHan.crm.workbench.controller;

import com.LittleHan.crm.exception.*;
import com.LittleHan.crm.settings.entity.User;
import com.LittleHan.crm.settings.service.UserService;
import com.LittleHan.crm.utils.DateTimeUtil;
import com.LittleHan.crm.utils.UUIDUtil;
import com.LittleHan.crm.vo.PaginationVo;
import com.LittleHan.crm.workbench.entity.*;
import com.LittleHan.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller(value = "clueController")
@RequestMapping(value="/clueCon")
public class ClueController {
    private static final String EDIT_Flag_No="0";
    private static final String EDIT_Flag_Yes="1";
    @Autowired
    @Qualifier(value = "clueServiceImpl")
    ClueService clueService;
    @Autowired
    @Qualifier(value = "userServiceImpl")
    UserService userService;
    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //所有者
    @RequestMapping(value="/getOwner.do")
    @ResponseBody
    public List<User> getUserList(){
        List<User> list=userService.activityModelGetUserList();
        return list;
    }
    //创建
    @RequestMapping(value="/saveClue.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> saveClue(Clue clue, HttpServletRequest request) throws ClueInsertException {
        clue.setId(UUIDUtil.getUUID());
        clue.setCreateBy(((User)request.getSession().getAttribute("user")).getName());
        clue.setCreateTime(DateTimeUtil.getSysTime());
        Map<String,String> map=new HashMap<>();
        map.put("yesOrNo","yes");
        clueService.clueSave(clue);
        return map;
    }

    //线索-查询信息
    @RequestMapping(value="/searchClue.do",method = RequestMethod.POST)
    @ResponseBody
    public void searchClue(){

    }

    //pageList
    @RequestMapping(value="/pageListCon.do")
    @ResponseBody
    public PaginationVo<Clue> pageListCon(String fullname,String company,String phone,String source,
                                          String owner,String mphone,String state,String pageNo,String pageSize){
        int pn=Integer.valueOf(pageNo);
        int ps=Integer.valueOf(pageSize);
        int start=(pn-1)*ps;
        PaginationVo<Clue> paginationVo=clueService.getTotalAndClueList(fullname,company,phone,source,owner,mphone,state,start,ps);
        List<Clue> list=paginationVo.getDataList();
        return paginationVo;
    }

    //修改，获取拥有者下拉列表信息及其他信息
    @RequestMapping(value="/updateGetInfCon.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateGetInfCon(String id){
        List<User> ownerList=userService.activityModelGetUserList();
        Clue clue=clueService.clueGet(id);
        Map<String,Object> map=new HashMap<>();
        map.put("ownerList",ownerList);
        map.put("clue",clue);
        return map;
    }

    //修改-更新按钮
    @RequestMapping(value="/updateCon.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> updateCon(Clue clue,HttpServletRequest req) throws ClueUpdateException {
        clue.setEditBy(((User)req.getSession().getAttribute("user")).getName());
        clue.setEditTime(DateTimeUtil.getSysTime());
        clueService.clueUpdate(clue);
        Map<String,String> map=new HashMap<>();
        map.put("yesOrNo","yes");
        return map;
    }

    //删除按钮
    @RequestMapping(value = "/delCon.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delCon(String []id){
        clueService.clueDel(id);
        /*for(String s:id){
            System.out.println("id:"+s);
        }*/
        Map<String,String> map=new HashMap<>();
        map.put("yesOrNo","yes");
        return map;
    }

    //详细信息页
    @RequestMapping(value="/toDetail.do")
    public ModelAndView toDetail(String id){
        ModelAndView mv=new ModelAndView();
        Clue clue=clueService.getClueToInf(id);
        mv.addObject("clue",clue);
        mv.setViewName("/workbench/clue/detail.jsp");
        return mv;
    }

    //详细信息页备注
    @RequestMapping(value="/remarkClue.do",method = RequestMethod.POST)
    @ResponseBody
    public List<ClueRemark> remarkClue(String id){
        List<ClueRemark> list=clueService.getClueRemarkList(id);
        return list;
    }

    //详细信息备注添加
    @RequestMapping(value="/remarkAdd.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remarkClueAdd(String noteContent,String clueId,HttpServletRequest req) throws ClueRemarkAddException {
        ClueRemark clueRemark=new ClueRemark();
        clueRemark.setId(UUIDUtil.getUUID());
        clueRemark.setNoteContent(noteContent);
        clueRemark.setCreateBy(((User)req.getSession().getAttribute("user")).getName());
        clueRemark.setCreateTime(DateTimeUtil.getSysTime());
        clueRemark.setEditFlag(EDIT_Flag_No);
        clueRemark.setClueId(clueId);
        clueService.clueRemarkAdd(clueRemark);
        Map<String,Object> map=new HashMap<>();
        map.put("yesOrNo","yes");
        map.put("clueRemark",clueRemark);
        return map;
    }

    //详细信息备注删除
    @RequestMapping(value="/remarkDel.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> remarkClueDel(String id) throws ClueRemarkDelException {
        clueService.clueRemarkDel(id);
        Map<String,String> map=new HashMap<>();
        map.put("yesOrNo","yes");
        return map;
    }

    //详细信息备注修改
    @RequestMapping(value="/remarkEdit.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remarkClueEdit(String id,String noteContent,HttpServletRequest req) throws ClueRemarkUpdException {
        ClueRemark clueRemark=new ClueRemark();
        clueRemark.setId(id);
        clueRemark.setNoteContent(noteContent);
        clueRemark.setEditBy(((User)req.getSession().getAttribute("user")).getName());
        clueRemark.setEditTime(DateTimeUtil.getSysTime());
        clueRemark.setEditFlag(EDIT_Flag_Yes);
        clueService.clueRemarkUpd(clueRemark);
        Map<String,Object> map=new HashMap<>();
        map.put("yesOrNo","yes");
        map.put("clueRemark",clueRemark);
        return map;
    }

    //详细信息加载市场活动
    /*key:owner,value张三
				key:endDate,value2021-11-13
				key:name,value发传单1
				key:startDate,value2021-11-10
				key:owner,value张三
				key:endDate,value2021-11-13
				key:name,value发传单AAA
				key:startDate,value2021-11-10
				key:owner,value张三
				key:endDate,value2021-11-13
				key:name,value发传单2AA
				key:startDate,value2021-11-10*/
    @RequestMapping(value="/remarkActivity.do")
    @ResponseBody
    public List<Map<String,Object>> remarkClueActivity(String clueId){
        //System.out.println("@@@@@@cluId:"+clueId);
        List<Map<String,Object>> map=clueService.clueRemarkActivity(clueId);
        return map;
    }

    //线索详细信息市场活动-解除关联
    @RequestMapping(value="/remarkActivityRemove.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> remarkClueActivityRemove(String id)throws ClueRemarkActivityRemoveException{
        clueService.clueRemarkActivityRemove(id);
        Map<String,String> map=new HashMap<>();
        map.put("yesOrNo","yes");
        return map;
    }

    //线索详细信息市场活动+关联市场活动
    @RequestMapping(value="/remarkActivityLike.do",method = RequestMethod.POST)
    @ResponseBody
    public List<Activity> remarkClueActivityLike(String name,String clueId){
        List<Activity> list=clueService.clueRemarkActivitySearch(name,clueId);
        return list;
    }

    //线索详细信息市场活动+关联按钮
    @RequestMapping(value="/remarkClueInfRelevancy.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remarkClueInfRelevancy(String []activityId,String clueId) throws ClueRemarkRelevancyException{
        List<ClueActivityRelation> list=clueService.clueRemarkRel(activityId,clueId);
        Map<String,Object> map=new HashMap<>();
        map.put("yseOrNo","yes");
        map.put("list",list);
        return map;
    }

    @RequestMapping(value="/convertActivityInf.do")
    @ResponseBody
    public List<Activity> convertActivityInf(String name){
        List<Activity> list=clueService.convertActivityGet(name);
        return list;
    }

    @PostMapping (value = "/convertTransform.do")
    @ResponseBody
    public Map<String,String> convertTransform(String clueId,String amountOfMoney,String tradeName,
                                   String expectedClosingDate,String stage,String activityName,boolean flag,
                                   HttpServletRequest req){

        String str=((User)req.getSession().getAttribute("user")).getName();

        Tran tran=null;
        if(!flag){
            tran=new Tran();
            tran.setMoney(amountOfMoney);
            tran.setName(tradeName);
            tran.setExpectedDate(expectedClosingDate);
            tran.setStage(stage);
            tran.setActivityId(activityName);
        }
        clueService.makeTransform(tran,clueId,str);
        Map<String,String> map=new HashMap<>();
        map.put("info","转换成功");

        return map;
    }


}
